/**
 * www.yiji.com
 *
 * RandomUtil.java, Create on 2008-8-22
 */
package com.lanmei.group.weixin.utils;

import java.util.Random;

/**
 * 随机数工具类
 *
 * @author zhaorui
 */
public class RandomUtil {
	private final static Random random = new Random();
	private final static char CHAR_SEED = 'a';
	private final static int CHAR_NUM = 26;

	private RandomUtil() {}
	
	/**
	 * 得到0-i之间的随机数
	 * @param 随机最大值
	 */
	public static int getInt(int i) {
//		random.setSeed(System.currentTimeMillis());
		return random.nextInt(i);
	}

	/**
	 * 得到0-i之间的随机数的固定长度的字符串
	 * 
	 * 例如：随机最大值是9，长度3，那么返回的结果可能是009,010,001
	 * @param i  随机最大值
	 * @param len 长度
	 */
	public static String getIntString(int i, int len) {
//		String val = String.valueOf(getInt(i));
//		if (val.length() > len)
//			return "";
//		else if (val.length() == len)
//			return val;
//		else {
//			int zeroLen = len - val.length();
//			StringBuilder sb = new StringBuilder();
//			for (int j = 0; j < zeroLen; j++) {
		
//				sb.append(0);
//			}
//			sb.append(val);
//			return sb.toString();
//		}
		if( i<= 0 || len <=0) return "";
		Random r = new Random();
		String str = "";
		for (int j = 0; j < len; j++) {
			str += String.valueOf(r.nextInt(i));
		}
		if (str.length()>len) {
			str = str.substring(0, len);
		}
		return str;
	}

	public static String getString(int len) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < len; i++) {
			sb.append((char) (getInt(CHAR_NUM) + CHAR_SEED));
		}
		return sb.toString();
	}

	/**
	 * 取两个数之间的随机数
	 */
	public static int getInt(int start, int end) {
		return getInt((end - start)) + start;
	}

}
